package com.example.mydy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.R;
import com.example.mydy.model.RewardTask;

import java.util.List;

/**
 * 我的悬赏任务列表适配器
 */
public class MyRewardTasksAdapter extends RecyclerView.Adapter<MyRewardTasksAdapter.ViewHolder> {

    private Context context;
    private List<RewardTask> tasks;
    private OnTaskActionListener listener;

    public interface OnTaskActionListener {
        void onTaskSelected(RewardTask task);
        void onTaskSubmit(RewardTask task);
        void onTaskCancel(RewardTask task);
    }

    public MyRewardTasksAdapter(Context context, List<RewardTask> tasks, OnTaskActionListener listener) {
        this.context = context;
        this.tasks = tasks;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_my_reward_task, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        RewardTask task = tasks.get(position);
        
        // 设置任务标题
        holder.tvTitle.setText(task.getTitle());
        
        // 设置任务描述
        holder.tvDescription.setText(task.getDescription());
        
        // 设置任务奖励
        holder.tvReward.setText(String.format("¥%.2f", task.getReward()));
        
        // 设置任务状态
        holder.tvStatus.setText(task.getProgressStatusText());
        
        // 根据任务状态设置按钮可见性
        switch (task.getProgressStatus()) {
            case RewardTask.STATUS_IN_PROGRESS:
                // 进行中的任务，显示提交和取消按钮
                holder.btnSubmit.setVisibility(View.VISIBLE);
                holder.btnCancel.setVisibility(View.VISIBLE);
                holder.tvSubmittedContent.setVisibility(View.GONE);
                holder.tvReviewComment.setVisibility(View.GONE);
                break;
                
            case RewardTask.STATUS_UNDER_REVIEW:
                // 审核中的任务，隐藏所有按钮，显示提交内容
                holder.btnSubmit.setVisibility(View.GONE);
                holder.btnCancel.setVisibility(View.GONE);
                holder.tvSubmittedContent.setVisibility(View.VISIBLE);
                holder.tvSubmittedContent.setText("提交内容: " + task.getSubmissionContent());
                holder.tvReviewComment.setVisibility(View.GONE);
                break;
                
            case RewardTask.STATUS_COMPLETED:
                // 已完成的任务，隐藏所有按钮，显示提交内容和评语
                holder.btnSubmit.setVisibility(View.GONE);
                holder.btnCancel.setVisibility(View.GONE);
                holder.tvSubmittedContent.setVisibility(View.VISIBLE);
                holder.tvSubmittedContent.setText("提交内容: " + task.getSubmissionContent());
                holder.tvReviewComment.setVisibility(View.VISIBLE);
                holder.tvReviewComment.setText("审核评语: " + task.getReviewComment());
                break;
                
            case RewardTask.STATUS_REJECTED:
                // 被拒绝的任务，显示提交按钮（重新提交），隐藏取消按钮
                holder.btnSubmit.setVisibility(View.VISIBLE);
                holder.btnSubmit.setText("重新提交");
                holder.btnCancel.setVisibility(View.GONE);
                holder.tvSubmittedContent.setVisibility(View.VISIBLE);
                holder.tvSubmittedContent.setText("提交内容: " + task.getSubmissionContent());
                holder.tvReviewComment.setVisibility(View.VISIBLE);
                holder.tvReviewComment.setText("拒绝原因: " + task.getReviewComment());
                break;
        }
        
        // 设置按钮点击事件
        holder.btnSubmit.setOnClickListener(v -> {
            if (listener != null) {
                listener.onTaskSubmit(task);
            }
        });
        
        holder.btnCancel.setOnClickListener(v -> {
            if (listener != null) {
                listener.onTaskCancel(task);
            }
        });
        
        // 设置整个条目的点击事件
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onTaskSelected(task);
            }
        });
    }

    @Override
    public int getItemCount() {
        return tasks.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvTitle;
        TextView tvDescription;
        TextView tvReward;
        TextView tvStatus;
        TextView tvSubmittedContent;
        TextView tvReviewComment;
        Button btnSubmit;
        Button btnCancel;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_task_title);
            tvDescription = itemView.findViewById(R.id.tv_task_description);
            tvReward = itemView.findViewById(R.id.tv_reward);
            tvStatus = itemView.findViewById(R.id.tv_status);
            tvSubmittedContent = itemView.findViewById(R.id.tv_submitted_content);
            tvReviewComment = itemView.findViewById(R.id.tv_review_comment);
            btnSubmit = itemView.findViewById(R.id.btn_submit);
            btnCancel = itemView.findViewById(R.id.btn_cancel);
        }
    }
} 